var JaikoolUI = new (function() {
	this.init = function() {
		// http://developer.mozilla.org/en/docs/Code_snippets:Tabbed_browser
		this.mainWindow = window
				.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
				.getInterface(Components.interfaces.nsIWebNavigation)
				.QueryInterface(Components.interfaces.nsIDocShellTreeItem).rootTreeItem
				.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
				.getInterface(Components.interfaces.nsIDOMWindow);
		if (this.mainWindow.jaikuapi) {
			// use same reference
			this.jaikuapi = this.mainWindow.jaikuapi;
		}
		this.sidebarBox = this.mainWindow.document
				.getElementById('sidebar-box');
		this.tabs = _gel("jk_msg_tabs");
		this.iconbox = _gel("jk_icon_box");
		this.addicon = _gel("jk_add_icon");
		this.input_text = _gel("jk_input_text");
		this.input_icon = _gel("jk_msg_icon");
		this.msg_bundle = _gel("jk_strings");
		this.msg_length_left = _gel("jk_msg_length_left");
		if (isDef(iconpack)) {
			for (var i = 0; i < iconpack.length; i++) {
				this.iconbox.addIcon(iconpack[i]);
			}
		}
		$(this.input_text).focus(this.onInputText).bind("input",
				this.onInputKeyUp);
	};
	this.onInputText = function(event) {
		var iss = JaikoolUI.input_text.getAttribute("isstatus");
		if (iss == "true") {
			JaikoolUI.input_text.value = "";
			JaikoolUI.input_text.setAttribute("isstatus", "false");
			JaikoolUI.input_text.maxLength = parseInt(JaikoolUI.msg_bundle
					.getString("jk_msg_max_length"), 10);
			_gel("jk_input_post").disabled = false;
			JaikoolUI.msg_length_left.disabled = false;
			JaikoolUI.onInputKeyUp(event);
		}
	}
	this.onInputKeyUp = function(event) {
		var max = parseInt(JaikoolUI.msg_bundle.getString("jk_msg_max_length"),
				10);
		var current = parseInt(JaikoolUI.input_text.value.length, 10);
		var diff = (max - current);
		if (diff < 0) {
			JaikoolUI.input_text.value = JaikoolUI.input_text.value.substring(
					0, max);
			diff = 0;
		}
		JaikoolUI.msg_length_left.value = JaikoolUI.msg_bundle
				.getFormattedString("jk_msg_length_left", [diff]);

	}
	this.refreshPresence = function(type) {
		this.jaikuapi.refreshPresence(type, function(data) {
			JaikoolUI.refreshPresenceCallback(data, type);
		});
	}
	this.refreshCommand = function() {
		_gel("jk_status_progress").collapsed = false;
		_gel("jk_input_post").disabled = true;
		_gel("jk_status_refresh").disabled = true;
		// this.refreshPresence(tabs.selectedIndex);
		this.refreshPresenceCallback(mockdata, this.tabs.selectedIndex);
	}
	this.refreshPresenceCallback = function(data, selectedIndex) {
		var mbox = JaikoolUI.tabs.selectTab(selectedIndex);
		if (mbox) {
			$.each(data.stream, function(i, item) {
				mbox.addMessage(item);
			});
		}
		_gel("jk_input_post").disabled = false;
		_gel("jk_status_refresh").disabled = false;
		_gel("jk_status_progress").collapsed = true;
	}
	this.toggleIconBox = function() {
		if (!this.iconbox.collapsed) {
			this.addicon.value = "+ Add Icon";
		} else {
			this.addicon.value = "- Add Icon";
		}
		this.iconbox.collapsed = (this.iconbox.collapsed ? false : true);
	}
	this.sendCommand = function() {
		var msg = this.input_text.value;
		if (!msg) {
			return;
		}
		var icon = this.input_icon.iconid;
		this.input_text.readonly = true;
		_gel("jk_input_post").disabled = true;
		_gel("jk_status_refresh").disabled = true;
		// TODO: add location setting box
		var location = null;
		var generated;
		this.jaikuapi.send(msg, icon, location, generated, this.sendCallback);
			// this.sendCallback({
			// status : 'ok'
			// });
	}
	this.sendCallback = function(data) {
		JaikoolUI.input_text.value = data.status;
		JaikoolUI.input_text.setAttribute("isstatus", "true");
		_gel("jk_status_refresh").disabled = false;
		_gel("jk_status_progress").collapsed = true;
	}
	this.fixMaxWidth = function(e) {
		if (JaikoolUI.tabs.currentTab) {
			JaikoolUI.tabs.currentTab.updateMaxWidth();
		}
	}
})();

window.addEventListener("resize", function(e) {
	jk_logger.info("resizing.." + e);
	JaikoolUI.fixMaxWidth(e);
}, false);

window.addEventListener("load", function(e) {
	JaikoolUI.init();
		// JaikoolUI.fixMaxWidth(e);
	}, false);
